﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace _使用参数化执行SQL语句
{
    /// <summary>
    /// SQL工具类
    /// </summary>
    public class SqlUtil
    {
        /// <summary>
        /// 执行方法 并返回受影响行数
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        /// <param name="commandType">SQL语句类型</param>
        /// <param name="commandText">SQL语句</param>
        /// <param name="para">参数化参数</param>
        /// <returns>受影响行数</returns>
        public static int ExecuteNonQuery(string connStr, CommandType commandType, string commandText, params SqlParameter[] para)
        {
            int res = int.MinValue;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    cmd.CommandTimeout = 60;
                    res = cmd.ExecuteNonQuery();
                }
            }
            return res;
        }

        /// <summary>
        /// 执行SQL语句并返回第一行第一列
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        /// <param name="commandType">SQL语句类型</param>
        /// <param name="commandText">SQL语句</param>
        /// <param name="para">参数</param>
        /// <returns>第一行第一列的值(object类型)</returns>
        public static object ExecuteScalar(string connStr, CommandType commandType, string commandText, params SqlParameter[] para)
        {
            object res = new object();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    cmd.CommandTimeout = 60;
                    res = cmd.ExecuteScalar();
                }
            }
            return res;
        }

        /// <summary>
        /// 返回List&lt;T&gt;数据
        /// </summary>
        /// <typeparam name="T">泛型类型(规定为Class类型)</typeparam>
        /// <param name="connStr">连接字符串</param>
        /// <param name="commandType">执行类型</param>
        /// <param name="commandText">SQL语句</param>
        /// <param name="para">参数</param>
        /// <returns>List</returns>
        public static List<T> GetList<T>(string connStr, CommandType commandType, string commandText, params SqlParameter[] para) where T : class, new()
        {
            DataTable dt = new DataTable("Result");
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    cmd.CommandTimeout = 60;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    sda.Dispose();
                }
            }

            List<T> Data = new List<T>();

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertyInfos = t.GetType().GetProperties();

                foreach (PropertyInfo item in propertyInfos)
                {
                    string TName = item.Name;
                    if (dt.Columns.Contains(TName))
                    {
                        object value = dr[TName];
                        if (value != null && value != DBNull.Value)
                            item.SetValue(t, value, null);

                    }
                }
                Data.Add(t);
            }
            return Data;
        }

        /// <summary>
        /// 返回DataTable数据
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        /// <param name="commandType">执行类型</param>
        /// <param name="commandText">SQL语句</param>
        /// <param name="para">参数</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTable(string connStr, CommandType commandType, string commandText, params SqlParameter[] para)
        {
            DataTable dt = new DataTable("result");
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    cmd.CommandTimeout = 60;
                    cmd.Parameters.AddRange(para);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    sda.Dispose();
                }

            }
            return dt;
        }

    }
}
